﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Basics.ShoppingOptionParameters;
using shenghua.Basics.ShoppingOptionParameterSetting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.ShoppingOptionParameterSetting
{
    [AbpAuthorize(PermissionNames.CommodityShoppingOptionManagement)]
    public class ShoppingOptionParameterAppService: shenghuaAppServiceBase, IShoppingOptionParameterAppService
    {
        private readonly ShoppingOptionParameters.ShoppingOptionParameterManager _shoppingOptionParameterManager;

        public ShoppingOptionParameterAppService(
            ShoppingOptionParameters.ShoppingOptionParameterManager shoppingOptionParameterManager)
        {
            _shoppingOptionParameterManager = shoppingOptionParameterManager;
        }

        public async Task<ShoppingOptionParameterDto> Create(CreateShoppingOptionParameterDto input)
        {
            ShoppingOptionParameter shoppingOptionParameter = new ShoppingOptionParameter
            {
                Name = input.Name,
                DisplayName = input.Name,
            };
            await _shoppingOptionParameterManager.Add(shoppingOptionParameter);
            return ObjectMapper.Map<ShoppingOptionParameterDto>(shoppingOptionParameter);
        }

        public async Task<ShoppingOptionParameterDto> Update(UpdateShoppingOptionParameterDto input)
        {
            ShoppingOptionParameter shoppingOptionParameter = await _shoppingOptionParameterManager.Get(input.Id);
            shoppingOptionParameter.Name = input.Name;
            shoppingOptionParameter.DisplayName = input.Name;
            await _shoppingOptionParameterManager.Update(shoppingOptionParameter);
            return ObjectMapper.Map<ShoppingOptionParameterDto>(shoppingOptionParameter);
        }

        public async Task SortUp(EntityDto input)
        {
            await _shoppingOptionParameterManager.SortUp(input.Id);
        }

        public async Task SortDown(EntityDto input)
        {
            await _shoppingOptionParameterManager.SortDown(input.Id);
        }

        [HttpPost]
        public async Task Delete(EntityDto input)
        {
            await _shoppingOptionParameterManager.Delete(input.Id);
        }

        public async Task<List<ShoppingOptionParameterDto>> GetAll()
        {
            List<ShoppingOptionParameter> shoppingOptionParameters = await _shoppingOptionParameterManager.GetAll();
            return ObjectMapper.Map<List<ShoppingOptionParameterDto>>(shoppingOptionParameters);
        }
    }
}
